<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>02 数据驱动</title>
</head>
<body>
    <div>
        <div>商品数量<input type="text" class="goodnum"></div>
        <div>商品价格<input type="text" class="goodprice"></div>
        <div>小计<span class="total"></span></div>
    </div>
    
</body>

<script>

    let goodnumInput = document.getElementsByClassName("goodnum")[0];
    let goodpriceInput  = document.getElementsByClassName("goodprice")[0];
    let total = document.getElementsByClassName("total")[0];

    let goodsinfo = {}  //定义一个空对象  存值

    Object.defineProperty(goodsinfo,"goodnum",{
        set(n){
            _goodnum = n;
            complier();
        },
        get(){
            return _goodnum;
        }
    })

    Object.defineProperty(goodsinfo,"goodprice",{
        set(n){
            _goodprice = n
            // console.log(_goodprice)
            complier();
        },
        get(){
            return _goodprice;
        }
    })







    goodnumInput.oninput = function(){
        goodsinfo.goodnum = this.value;
        
    }

    goodpriceInput.oninput = function(){
        goodsinfo.goodprice = this.value;
    }

    
    function complier(){
        total.innerText = String(goodsinfo.goodnum*goodsinfo.goodprice);
    }




</script>
</html>